﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.SeoConfig
{
    public partial class EditSeoConfig : BasePage
    {
        private readonly BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        private readonly BLL.NJL_SEO_CONFIG bll = new BLL.NJL_SEO_CONFIG();
        protected Model.NJL_SEO_CONFIG model;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
            ddlProvince.DataSource = ds;
            ddlProvince.DataTextField = "AREA_NAME";
            ddlProvince.DataValueField = "AREA_CODE";
            ddlProvince.DataBind();
            if (WEBSITE_CODE == "1")
            {

            }
            else
            {
                ddlProvince.Visible = false;
                string ProvinceId = WEBSITE_CODE.Substring(0, 2);
                DataSet dscity = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                ddlCity.DataSource = dscity;
                ddlCity.DataTextField = "AREA_NAME";
                ddlCity.DataValueField = "AREA_CODE";
                ddlCity.DataBind();

                ddlCity.Items.Add(new ListItem("全省", "0"));
            }


            //数据加载
            model = bll.GetModel(Request.QueryString["id"]);

            this.txtTitle.Text = model.SEO_NAME;
            this.txtSeoTitle.Text = model.SEO_TITLE;
            this.txtSeoKeywords.Text = model.SEO_KEYWORDS;
            this.txtSeoDescription.Text = model.SEO_DESCRIPTION;
            if (model.AREA_CODE != "0")
            {
                ddlProvince.Items.FindByValue(model.AREA_CODE.Substring(0, 2)+"0000").Selected = true;
            }
            ddlCity.Items.FindByValue(model.AREA_CODE).Selected = true;

        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProvinceId = ddlProvince.SelectedValue.Substring(0, 2);
            DataSet ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "AREA_NAME";
            ddlCity.DataValueField = "AREA_CODE";
            ddlCity.DataBind();

            ddlCity.Items.Add(new ListItem("全省", "0"));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //判断名称是否重复
            IList<Model.NJL_SEO_CONFIG> list = bll.GetModelList("NJL_SEO_CONFIG.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_SEO_CONFIG.SEO_NAME='" + txtTitle.Text.Trim() + "' AND NJL_SEO_CONFIG.AREA_CODE = '" + ddlCity.SelectedValue + "'");

            if (list.Count > 0 && !list[0].ID.Equals(Request.QueryString["id"]))
            {
                Royalsoft.Common.MessageBox.Show(this, "栏目名称已存在，请换一个再提交");
                return;
            }

            model = bll.GetModel(Request.QueryString["id"]);

            //model.NJL_SYS_BASE_ID = WEBSITE_CODE;
            model.AREA_CODE = ddlCity.SelectedItem.Value;
            model.SEO_NAME = txtTitle.Text.Trim();
            model.SEO_TITLE = txtSeoTitle.Text.Trim();
            model.SEO_KEYWORDS = txtSeoKeywords.Text.Trim();
            model.SEO_DESCRIPTION = txtSeoDescription.Text.Trim();

            model.UPDATE_DATE_TIME = DateTime.Now;
            model.UPDATE_USER_ID = USER_ID;
            model.UPDATE_USER_NAME = USER_NAME;

            if (bll.Update(model))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "修改成功", Request.Url.ToString());
            }
        }
    }
}